package interceptor;

import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import admin.model.AdminInfo;

import static org.apache.struts2.StrutsStatics.HTTP_REQUEST;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.interceptor.Interceptor; 

public class LoginSessionCheckInterceptor implements Interceptor{

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext context = invocation.getInvocationContext();
        Map session = context.getSession();

        if (session.get("userId") == null) {
            HttpServletRequest request = (HttpServletRequest)context.get(HTTP_REQUEST);
            String queryString = request.getQueryString();
            String currentURI = request.getServletPath() + (queryString != null ?"?"+queryString : "");
            context.getSession().put("url", currentURI);
            return Action.LOGIN;
        }
        return invocation.invoke();
	}	
}
